package testconfig

import (
	"context"
	"yunzhan/alert-server/internal/model/notifiers"

	"github.com/zeromicro/go-zero/core/logx"
	"yunzhan/alert-server/internal/svc"
	"yunzhan/alert-server/internal/types"
)

type TestEmailLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

// NewTestEmailLogic 邮件测试
func NewTestEmailLogic(ctx context.Context, svcCtx *svc.ServiceContext) *TestEmailLogic {
	return &TestEmailLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *TestEmailLogic) TestEmail(req *types.TestEmailRequest) (resp *types.TestResponse, err error) {
	if len(req.Config) == 0 {
		return &types.TestResponse{
			Success: 500,
			Message: "未提供邮件配置",
		}, err
	}

	//config := req.Config[0]

	//m := gomail.NewMessage()
	//m.SetHeader("From", config.Username)
	//m.SetHeader("To", config.Recipient)
	//m.SetHeader("Subject", "yunfan-bigdataOps 测试邮件")

	// 美化和补充邮件内容
	body := `
		<!DOCTYPE html>
		<html lang="zh-CN">
		<head>
			<meta charset="UTF-8">
			<title>测试邮件</title>
		</head>
		<body>
			<h2>尊敬的用户，您好！</h2>
			<p>这是一封由 <strong>yunfan-bigdataOps</strong> 发送的测试邮件。</p>
			<p>如果您收到这封邮件，说明邮件发送功能正常。</p>
			<br>
			<p>此致</p>
			<p>yunfan-bigdataOps 团队</p>
		</body>
		</html>
	`
	err = notifiers.SendEmail(req.Config, "text/html", body)
	if err != nil {
		return &types.TestResponse{
			Success: 500,
			Message: err.Error(),
		}, err
	}

	//m.SetBody("text/html", body)
	//port, err := strconv.Atoi(config.Port)
	//if err != nil {
	//	return &types.TestResponse{
	//		Success: 500,
	//		Message: err.Error(),
	//	}, nil
	//}
	//d := gomail.NewDialer(config.SmtpServer, port, config.Username, config.Password)
	//
	//// 发送邮件
	//if err := d.DialAndSend(m); err != nil {
	//	return &types.TestResponse{
	//		Success: 500,
	//		Message: err.Error(),
	//	}, err
	//}

	return &types.TestResponse{
		Success: 200,
		Message: "邮件发送成功!",
	}, nil
}
